package com.yfbao.horizon.utils;

public class SensitiveWordFilter {
    private TrieNode root = new TrieNode();

    public void addSensitiveWord(String word) {
        TrieNode current = root;
        for (char c : word.toCharArray()) {
            if (current.getChild(c)==null) {
                current.addChild(c);
            }
            current = current.getChild(c);
        }
        current.setEndOfWord(true);
    }

    public boolean containsSensitiveWord(String text) {
        TrieNode current = root;
        for (char c : text.toCharArray()) {
            current = current.getChild(c);
            if (current == null) {
                current = root;
            }
            if (current.isEndOfWord()) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        SensitiveWordFilter filter = new SensitiveWordFilter();

        // 添加敏感词
        filter.addSensitiveWord("bad");
        filter.addSensitiveWord("evil");

        // 测试文本
        String text = "This is a bad example. It contains evil content.";


        // 检查文本中是否包含敏感词
        if (filter.containsSensitiveWord(text)) {
            System.out.println("The text contains sensitive words.");
        } else {
            System.out.println("The text is clean.");
        }
    }
}
